<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="维护站编号" prop="wxCode">
        <el-input
          v-model="queryParams.wxCode"
          placeholder="请输入维护站编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维护站名称" prop="wxUnit">
        <el-select v-model="queryParams.wxUnit" filterable placeholder="请选择">
          <el-option
            v-for="item in wxUnitList"
            :key="item.id"
            :label="item.wxUnit"
            :value="item.wxUnit">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"

          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vehicleservice:secondary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"

          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vehicleservice:secondary:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"

          icon="el-icon-upload2"
          size="mini"
          @click="openImport"
          v-hasPermi="['vehicleservice:secondary:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  border v-loading="loading" :data="secondaryList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="维护站编号" align="center" prop="wxCode" />
      <el-table-column label="维护站名称" align="center" prop="wxUnit" />
      <el-table-column label="企业类型" align="center" prop="enterpriseType" >
        <template slot-scope="scope">
          <span v-if="scope.row.enterpriseType === 1">维修企业</span>
          <span v-if="scope.row.enterpriseType === 2">营运车辆企业</span>
          <span v-if="scope.row.enterpriseType === 3">综合性能企业</span>
        </template>
      </el-table-column>
      <el-table-column label="维护站地址" align="center" prop="wxAddr" />
      <el-table-column label="联系人" align="center" prop="wxPerson" />
      <el-table-column label="联系电话" align="center" prop="wxTel" />
      <el-table-column v-if="controlShow" label="站点控制" align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch
            @change="updateStatus(scope.row.id)"
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="统一社会信用编码" width="130" align="center" prop="creditCode" />
      <el-table-column label="营业执照照片" align="center" prop="licensepic" >
        <template slot-scope="scope">
          <el-image style="width: 70px; height: 70px" :src="scope.row.licensepic" :preview-src-list="[scope.row.licensepic]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="法人电话" align="center" prop="legalTel" />
      <el-table-column label="经营范围" align="center" prop="businessScope" />
      <el-table-column label="所属区县" align="center" prop="orgCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicleservice:secondary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vehicleservice:secondary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--导入-->
    <el-dialog
      title="提示"
      :visible.sync="openImportData"
      width="30%"
      :before-close="handleClose">
      <el-upload
        class="upload-demo"
        :action="importUrl"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success="uploadSuccess"
        :limit="1"
        :headers="headers"
        :accept="accept"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传.xls /.xlsx文件</div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openImportData = false">取 消</el-button>
        <el-button type="primary" @click="openImportData = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加或修改汽车检测-二级维护站对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="dilogleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="维护站名称" prop="wxUnit">
          <el-input v-model="form.wxUnit"  maxlength="50" placeholder="请输入维护站名称" />
        </el-form-item>
        <el-form-item label="企业类型" prop="enterpriseType">
          <el-select  v-model="form.enterpriseType" placeholder="请选择企业类型" style="width: 100%;">
            <el-option  label="维修企业" :value="1"></el-option>
            <el-option  label="营运车辆企业" :value="2"></el-option>
            <el-option  label="综合性能企业" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护站地址" prop="wxAddr">
          <el-input v-model="form.wxAddr"  maxlength="100" placeholder="请输入维护站地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="wxPerson">
          <el-input v-model="form.wxPerson" placeholder="请输入联系人"  maxlength="20" />
        </el-form-item>
        <el-form-item label="联系电话" prop="wxTel">
          <el-input v-model="form.wxTel" placeholder="请输入联系电话"  maxlength="20" />
        </el-form-item>
        <el-form-item label="所属区县" required>
          <el-cascader  v-model="form.orgCodes" :props="props" style="width: 100%" :options="options" :show-all-levels="true"></el-cascader>
        </el-form-item>
        <el-form-item label="统一社会信用编码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用编码"  maxlength="100" />
        </el-form-item>
        <el-form-item label="营业执照照片" required>
          <image-upload :limit="1" v-model="form.licensepic"></image-upload>
        </el-form-item>
        <el-form-item label="法人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入法人"  maxlength="20" />
        </el-form-item>
        <el-form-item label="法人电话" prop="legalTel">
          <el-input v-model="form.legalTel" placeholder="请输入法人电话"  maxlength="20" />
        </el-form-item>
        <el-form-item label="经营范围" prop="businessScope">
          <el-input type="textarea"  maxlength="200" show-word-limit :rows="4" v-model="form.businessScope" placeholder="请输入经营范围" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSecondary, getSecondary, delSecondary, addSecondary, updateSecondary, controlShow,updateStatus,wxUnitList } from "@/api/maintenanceenterprise/secondarysite";
import { getDistrictTreeList,selectOrgCodeName } from "@/api/system/district";
import { getToken } from "@/utils/auth";
export default {
  name: "Secondary",
  data() {
    return {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      controlShow: false,
      props: {
        label: "name",
        value: "code",
        children: "chridrenList"
      },
      wxUnitList: [],
      options: [],
      fileList: [],
      accept: ".xls,.xlsx",
      importUrl: process.env.VUE_APP_BASE_API + "/vehicleservice/secondary/import/data",
      openImportData: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 汽车检测-二级维护站表格数据
      secondaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wxCode: null,
        wxUnit: "",
        wxAddr: null,
        wxPerson: null,
        wxTel: null,
        creditCode: null,
        licensepic: null,
        legalPerson: null,
        legalTel: null,
        businessScope: null,
        orgCode: null,
        enterpriseType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wxUnit: [
          { required: true, message: '请输入维护站名称', trigger: 'blur' },
        ],
        wxAddr: [
          { required: true, message: '请输入维护站地址', trigger: 'blur' },
        ],
        wxPerson: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
        ],
        wxTel: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' },
        ],
        creditCode: [
          { required: true, message: '请输入社会统一代码', trigger: 'blur' },
        ],
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' },
        ],
        legalTel: [
          { required: true, message: '请输入法人电话', trigger: 'blur' },
        ],
        businessScope: [
          { required: true, message: '请输入经营范围', trigger: 'blur' }
        ],
        enterpriseType: [
          {required: true, message: '请选择企业类型', trigger: 'change'}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDistrictTreeList();
    this.controlShowFuncation();
    this.getWxUnitList();
  },
  methods: {
    getWxUnitList(){
      wxUnitList().then(res => {
        this.wxUnitList = res.data
      })
    },
    dilogleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
            done();
        })
        .catch(_ => {});
    },
    updateStatus(e){
      updateStatus(e).then(res => {
        this.$message.success(res.msg);
      })
    },
    controlShowFuncation(){
      controlShow().then(res => {
        this.controlShow = res.data
      })
    },
    getDistrictTreeList(){
      getDistrictTreeList().then(res => {
        this.options = res.data
      })
    },
    handleClose(){
      this.fileList = []
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    uploadSuccess(response, file, fileList){
      if(response.code === 200){
        this.$message.success(response.msg);
        this.openImportData = false;
        this.fileList = fileList;
        this.getList();
      }else {
        this.$message.error(response.msg);
      }
    },

    openImport(){
      this.openImportData = true;
      this.fileList = [];
    },
    /** 查询汽车检测-二级维护站列表 */
    getList() {
      this.loading = true;
      listSecondary(this.queryParams).then(response => {
        this.secondaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wxCode: null,
        wxUnit: null,
        wxAddr: null,
        wxPerson: null,
        wxTel: null,
        creditCode: null,
        licensepic: null,
        legalPerson: null,
        legalTel: null,
        businessScope: null,
        orgCode: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.orgCode) {
        this.queryParams.orgCode = this.queryParams.orgCodes.toString();
      }

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加二级维护站";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSecondary(id).then(response => {
        this.form = response.data;
        this.form.orgCodes = response.data.orgCode;
        this.open = true;
        this.title = "修改汽车检测-二级维护站";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.orgCode = this.form.orgCodes.toString();
          if (this.form.id != null) {
            updateSecondary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSecondary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delSecondary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicleservice/secondary/export', {
        ...this.queryParams
      }, `secondary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
